//Programs TABLE6A and TABLE6B produce the results in TABLE 6.
//The programs are intended to be run in tandem, with program A run first.
//It takes approximately 2.5 hours on a laptop to run 10,000 iterations.


etime, start
log using "\\file\UsersW$\wrr15\Home\My Documents\My Files\HURWICZ BIAS\REVISION FOR ECONOMIC MODELLING\PROGRAMS2\TABLE6.smcl", replace
drop _all
clear
set more off
set seed 13
matrix medLRP = J(5,4,0)
matrix confidLRP = J(5,8,0)
matrix meanRR = J(5,4,0)
// The local commands below set all the parameters for the experiments.
local beffect = 0 // This variable controls the degree of correlation between
                  // between x and the fixed effect.  When beffect = 0, there
				  // is no omitted variable bias.  As beffect increases,
				  // omitted variable bias increases.
local numN = 50 // This sets the number of cross-sectional units
local numT = 30 // This sets the number of time observations per unit
local numNT = `numN'*`numT' // This sets the total number of observations
local beta0 = 0 // This sets the intercept term
local betax = 1 // This sets the slope coefficient for x
local reps = 10000

local i = 1
foreach betay in 0.60 0.70 0.80 0.90 0.95 {
      simulate DFELRP = r(DFELRP) pDFELRP = r(pDFELRP) /// 
	     AHLRP = r(AHLRP) pAHLRP = r(pAHLRP) DGMMLRP = r(DGMMLRP) ///
	     pDGMMLRP = r(pDGMMLRP) SGMMLRP = r(SGMMLRP) pSGMMLRP = r(pSGMMLRP) , ///
         reps(`reps'): DPDprog, betay(`betay') betax(`betax') beta0(`beta0') ///
		 numN(`numN') numT(`numT') numNT(`numNT') beffect(`beffect')
		 
	  summ DFELRP, detail
      matrix medLRP[`i',1] = r(p50)
      matrix confidLRP[`i',1] = r(p5)
      matrix confidLRP[`i',2] = r(p95)
	  generate RRDFELRP = 0
      replace RRDFELRP = 1 if pDFELRP < 0.05  
	  summ RRDFELRP, meanonly
      matrix meanRR[`i', 1] = r(mean)
		 
		 
      summ AHLRP, detail
      matrix medLRP[`i',2] = r(p50)
      matrix confidLRP[`i',3] = r(p5)
      matrix confidLRP[`i',4] = r(p95)
	  generate RRAHLRP = 0
      replace RRAHLRP = 1 if pAHLRP < 0.05  
	  summ RRAHLRP, meanonly
      matrix meanRR[`i', 2] = r(mean)
	  
      summ DGMMLRP, detail
      matrix medLRP[`i',3] = r(p50)
      matrix confidLRP[`i',5] = r(p5)
      matrix confidLRP[`i',6] = r(p95)
	  generate RRDGMMLRP = 0
      replace RRDGMMLRP = 1 if pDGMMLRP < 0.05  
	  summ RRDGMMLRP, meanonly
      matrix meanRR[`i', 3] = r(mean)
	  	  
      summ SGMMLRP, detail
      matrix medLRP[`i',4] = r(p50)
      matrix confidLRP[`i',7] = r(p5)
      matrix confidLRP[`i',8] = r(p95)
	  generate RRSGMMLRP = 0
      replace RRSGMMLRP = 1 if pSGMMLRP < 0.05  
	  summ RRSGMMLRP, meanonly
      matrix meanRR[`i', 4] = r(mean)
	  
      local `++i'
   }

// These commands print out the results
matrix colnames medLRP = DFELRP(50) AHLRP(p50) DGMMLRP(p50) SGMMLRP(p50)
matrix rownames medLRP = B60 B70 B80 B90 B95
matrix colnames confidLRP = DFELRP(p5) DFELRP(p95) AHLRP(p5) AHLRP(p95) /// 
		DGMMLRP(p5) DGMMLRP(p95) SGMMLRP(p5) SGMMLRP(p95)
matrix rownames confidLRP = B60 B70 B80 B90 B95
matrix colnames meanRR = DFELRP AHLRP DGMMLRP  SGMMLRP
matrix rownames meanRR = B60 B70 B80 B90 B95
matrix list medLRP
matrix list confidLRP
matrix list meanRR


etime

log close
